home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / QSORT.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  7KB  |  229 lines

  1. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  2. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  3. ** Rochester NH, 03867-2954, USA.
  4. */
  5.  
  6. /*-
  7.  * Copyright (c) 1980, 1983 The Regents of the University of California.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms are permitted
  11.  * provided that: (1) source distributions retain this entire copyright
  12.  * notice and comment, and (2) distributions including binaries display
  13.  * the following acknowledgement:  ``This product includes software
  14.  * developed by the University of California, Berkeley and its contributors''
  15.  * in the documentation or other materials provided with the distribution
  16.  * and in all advertising materials mentioning features or use of this
  17.  * software. Neither the name of the University nor the names of its
  18.  * contributors may be used to endorse or promote products derived
  19.  * from this software without specific prior written permission.
  20.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  21.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  22.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23.  */
  24.  
  25. #if defined(LIBC_SCCS) && !defined(lint)
  26. static char sccsid[] = "@(#)qsort.c    5.7 (Berkeley) 5/17/90";
  27. #endif /* LIBC_SCCS and not lint */
  28.  
  29. #include <stdlib.h>
  30.  
  31. /*
  32.  * qsort.c:
  33.  * Our own version of the system qsort routine which is faster by an average
  34.  * of 25%, with lows and highs of 10% and 50%.
  35.  * The THRESHold below is the insertion sort threshold, and has been adjusted
  36.  * for records of size 48 bytes.
  37.  * The MTHREShold is where we stop finding a better median.
  38.  */
  39.  
  40. #define        THRESH        4        /* threshold for insertion */
  41. #define        MTHRESH        6        /* threshold for median */
  42.  
  43. static  int        (*qcmp)();        /* the comparison routine */
  44. static  int        qsz;            /* size of each record */
  45. static  int        thresh;            /* THRESHold in chars */
  46. static  int        mthresh;        /* MTHRESHold in chars */
  47.  
  48. /*
  49.  * qsort:
  50.  * First, set up some global parameters for qst to share.  Then, quicksort
  51.  * with qst(), and then a cleanup insertion sort ourselves.  Sound simple?
  52.  * It's not...
  53.  */
  54.  
  55. void
  56. qsort(base, n, size, compar)
  57.     void    *base;
  58.     int    n;
  59.     unsigned    size;
  60.     int    (*compar)(void*,void*);
  61. {
  62.     register char c, *i, *j, *lo, *hi;
  63.     char *min, *max;
  64.  
  65.     if (n <= 1)
  66.         return;
  67.     qsz = size;
  68.     qcmp = compar;
  69.     thresh = qsz * THRESH;
  70.     mthresh = qsz * MTHRESH;
  71.     max = base + n * qsz;
  72.     if (n >= THRESH) {
  73.         qst(base, max);
  74.         hi = base + thresh;
  75.     } else {
  76.         hi = max;
  77.     }
  78.     /*
  79.      * First put smallest element, which must be in the first THRESH, in
  80.      * the first position as a sentinel.  This is done just by searching
  81.      * the first THRESH elements (or the first n if n < THRESH), finding
  82.      * the min, and swapping it into the first position.
  83.      */
  84.     for (j = lo = base; (lo += qsz) < hi; )
  85.         if (qcmp(j, lo) > 0)
  86.             j = lo;
  87.     if (j != base) {
  88.         /* swap j into place */
  89.         for (i = base, hi = base + qsz; i < hi; ) {
  90.             c = *j;
  91.             *j++ = *i;
  92.             *i++ = c;
  93.         }
  94.     }
  95.     /*
  96.      * With our sentinel in place, we now run the following hyper-fast
  97.      * insertion sort.  For each remaining element, min, from [1] to [n-1],
  98.      * set hi to the index of the element AFTER which this one goes.
  99.      * Then, do the standard insertion sort shift on a character at a time
  100.      * basis for each element in the frob.
  101.      */
  102.     for (min = base; (hi = min += qsz) < max; ) {
  103.         while (qcmp(hi -= qsz, min) > 0)
  104.             /* void */;
  105.         if ((hi += qsz) != min) {
  106.             for (lo = min + qsz; --lo >= min; ) {
  107.                 c = *lo;
  108.                 for (i = j = lo; (j -= qsz) >= hi; i = j)
  109.                     *i = *j;
  110.                 *i = c;
  111.             }
  112.         }
  113.     }
  114. }
  115.  
  116. /*
  117.  * qst:
  118.  * Do a quicksort
  119.  * First, find the median element, and put that one in the first place as the
  120.  * discriminator.  (This "median" is just the median of the first, last and
  121.  * middle elements).  (Using this median instead of the first element is a big
  122.  * win).  Then, the usual partitioning/swapping, followed by moving the
  123.  * discriminator into the right place.  Then, figure out the sizes of the two
  124.  * partions, do the smaller one recursively and the larger one via a repeat of
  125.  * this code.  Stopping when there are less than THRESH elements in a partition
  126.  * and cleaning up with an insertion sort (in our caller) is a huge win.
  127.  * All data swaps are done in-line, which is space-losing but time-saving.
  128.  * (And there are only three places where this is done).
  129.  */
  130.  
  131. static
  132. qst(base, max)
  133.     char *base, *max;
  134. {
  135.     register char c, *i, *j, *jj;
  136.     register int ii;
  137.     char *mid, *tmp;
  138.     int lo, hi;
  139.  
  140.     /*
  141.      * At the top here, lo is the number of characters of elements in the
  142.      * current partition.  (Which should be max - base).
  143.      * Find the median of the first, last, and middle element and make
  144.      * that the middle element.  Set j to largest of first and middle.
  145.      * If max is larger than that guy, then it's that guy, else compare
  146.      * max with loser of first and take larger.  Things are set up to
  147.      * prefer the middle, then the first in case of ties.
  148.      */
  149.     lo = max - base;        /* number of elements as chars */
  150.     do    {
  151.         mid = i = base + qsz * ((lo / qsz) >> 1);
  152.         if (lo >= mthresh) {
  153.             j = (qcmp((jj = base), i) > 0 ? jj : i);
  154.             if (qcmp(j, (tmp = max - qsz)) > 0) {
  155.                 /* switch to first loser */
  156.                 j = (j == jj ? i : jj);
  157.                 if (qcmp(j, tmp) < 0)
  158.                     j = tmp;
  159.             }
  160.             if (j != i) {
  161.                 ii = qsz;
  162.                 do    {
  163.                     c = *i;
  164.                     *i++ = *j;
  165.                     *j++ = c;
  166.                 } while (--ii);
  167.             }
  168.         }
  169.         /*
  170.          * Semi-standard quicksort partitioning/swapping
  171.          */
  172.         for (i = base, j = max - qsz; ; ) {
  173.             while (i < mid && qcmp(i, mid) <= 0)
  174.                 i += qsz;
  175.             while (j > mid) {
  176.                 if (qcmp(mid, j) <= 0) {
  177.                     j -= qsz;
  178.                     continue;
  179.                 }
  180.                 tmp = i + qsz;    /* value of i after swap */
  181.                 if (i == mid) {
  182.                     /* j <-> mid, new mid is j */
  183.                     mid = jj = j;
  184.                 } else {
  185.                     /* i <-> j */
  186.                     jj = j;
  187.                     j -= qsz;
  188.                 }
  189.                 goto swap;
  190.             }
  191.             if (i == mid) {
  192.                 break;
  193.             } else {
  194.                 /* i <-> mid, new mid is i */
  195.                 jj = mid;
  196.                 tmp = mid = i;    /* value of i after swap */
  197.                 j -= qsz;
  198.             }
  199.         swap:
  200.             ii = qsz;
  201.             do    {
  202.                 c = *i;
  203.                 *i++ = *jj;
  204.                 *jj++ = c;
  205.             } while (--ii);
  206.             i = tmp;
  207.         }
  208.         /*
  209.          * Look at sizes of the two partitions, do the smaller
  210.          * one first by recursion, then do the larger one by
  211.          * making sure lo is its size, base and max are update
  212.          * correctly, and branching back.  But only repeat
  213.          * (recursively or by branching) if the partition is
  214.          * of at least size THRESH.
  215.          */
  216.         i = (j = mid) + qsz;
  217.         if ((lo = j - base) <= (hi = max - i)) {
  218.             if (lo >= thresh)
  219.                 qst(base, j);
  220.             base = i;
  221.             lo = hi;
  222.         } else {
  223.             if (hi >= thresh)
  224.                 qst(i, max);
  225.             max = j;
  226.         }
  227.     } while (lo >= thresh);
  228. }
  229.